package com.meta.platform.login.plus.security;

import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;

import java.util.Collection;

/**
 * 验证码认证
 *
 * @author pengjianfeng
 * @version 1.0
 * @date 2021/1/12 9:01 上午
 */
public class LoginSimpleAuthenticationToken extends AbstractAuthenticationToken {
    
    private static final long serialVersionUID = 2140722515712264796L;
    
    private final Object principal;
    
    public LoginSimpleAuthenticationToken(Object principal) {
        super(null);
        this.principal = principal;
        setAuthenticated(false);
    }
    
    public LoginSimpleAuthenticationToken(Object principal, Collection<? extends GrantedAuthority> authorities) {
        super(authorities);
        this.principal = principal;
        super.setAuthenticated(true);
    }
    
    @Override
    public Object getCredentials() {
        return null;
    }
    
    @Override
    public Object getPrincipal() {
        return this.principal;
    }
    
    @Override
    public void eraseCredentials() {
        super.eraseCredentials();
    }
    
    public void setAuthenticated(boolean isAuthenticated)
        throws IllegalArgumentException {
        if (isAuthenticated) {
            throw new IllegalArgumentException(
                "Cannot set this token to trusted - use constructor which takes a GrantedAuthority list instead");
        }
        
        super.setAuthenticated(false);
    }
}
